package com.example.wificovv2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.example.database.ListScan;
import com.example.database.MyArrayAdapter;
import com.example.database.MyWifi;
import com.example.database.myDBHelper;
import com.example.wificov.Innitialvaluepic;
import com.example.wificov.SamplingPowPic;
import android.net.wifi.ScanResult;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class WSTActivity extends Activity {
	myDBHelper db;
	
	MyWifi wifi;
	String stand;
	String ssid;
	SimpleAdapter adapter;
    int size = 0;
    String ITEM_KEY = "key";
	
	TextView macid, name, level, freq;	
	EditText ipserver, channel, user ;
	Button updateWifi, save, move1, move2;
	RadioButton g, n;
	ListView lvWifi=null;
	List<ScanResult> results;	
	ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
    

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wst);

		db = new myDBHelper(this);

		macid = (TextView)findViewById(R.id.macid);
		name = (TextView)findViewById(R.id.name);
		level = (TextView)findViewById(R.id.signal);
		freq = (TextView)findViewById(R.id.freq);
		channel = (EditText)findViewById(R.id.channel);
		ipserver = (EditText)findViewById(R.id.ipserver);
		user = (EditText)findViewById(R.id.numuser);
		
		updateWifi = (Button)findViewById(R.id.UpdateWifi);
		save = (Button) findViewById(R.id.savescan);
		move1 = (Button) findViewById(R.id.movedetailpic);
		move2 = (Button) findViewById(R.id.movedetailmap);
		
		lvWifi=(ListView) findViewById(R.id.lvwifi);
		g = (RadioButton) findViewById(R.id.g);
		n = (RadioButton) findViewById(R.id.n);
		
		// Chọn AP có giá trị APID tương ứng được chọn thuộc mapId cần khảo sát
		final int APID = Integer.parseInt(WSTActivity.this.getIntent().getStringExtra("APID"));
		final int APID2 = Integer.parseInt(WSTActivity.this.getIntent().getStringExtra("APID2"));
		
		// Thiết lập giá trị Scan wifi vào ListView
	    this.adapter = new SimpleAdapter(WSTActivity.this, arraylist, R.layout.myitemlayout, new String[] { ITEM_KEY }, new int[] { R.id.txtitem1 });
	    lvWifi.setAdapter(this.adapter);	    
		wifi = new MyWifi(this);		
		lvWifi.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			 public void onItemClick(
			 AdapterView<?> parent, View view, int position, long id) {
				 String key = "";
				 String value = "";
				 HashMap<String, String> item = (HashMap<String, String>) parent.getAdapter().getItem(position);
				 for (HashMap.Entry<String,String> entry : item.entrySet()) {
					//System.out.printf("%s -> %s%n", entry.getKey(), entry.getValue());
					// key = entry.getKey();
					 value = entry.getValue();
					 String[] separated = value.split(":");
					 macid.setText(separated[0]);
					 name.setText(separated[1]);
					 level.setText(separated[2]);
					 freq.setText(separated[3]);
					 //Match giá trị tần số
					 if (level.getText().toString() =="2.412" ){
						 channel.setText("1");
					 }
					 else if(level.getText().toString() =="2.417"){
						 channel.setText("2");
					 }
					 else if(level.getText().toString() =="2.422"){
						 channel.setText("3");
					 }
					 else if(level.getText().toString() =="2.427"){
						 channel.setText("4");
					 }
					 else if(level.getText().toString() =="2.432"){
						 channel.setText("5");
					 }
					 else if(level.getText().toString() =="2.437"){
						 channel.setText("6");
					 }
					 else if(level.getText().toString() =="2.442"){
						 channel.setText("7");
					 }
					 else if(level.getText().toString() =="2.447"){
						 channel.setText("8");
					 }
					 else if(level.getText().toString() =="2.452"){
						 channel.setText("9");
					 }
					 else if(level.getText().toString() =="2.457"){
						 channel.setText("10");
					 }
					 else
						 channel.setText("11");
				 }
				
			 }});
			
		//Luu cac gia tri vao database
		//Chuyen gia tri BSID sang activity tiep theo
		save.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(g.isChecked()){
					stand = "802.11g";
				} else stand = "802.11n";
				db.updateAPPrams(Integer.parseInt(channel.getText().toString()), Float.parseFloat(freq.getText().toString()), 
						stand, ipserver.getText().toString(), Integer.parseInt(user.getText().toString()), 
						APID);
				}});
		
		move1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
//				wifi.End();
				Intent intent = new Intent(WSTActivity.this,SamplingPowPic.class);				
				String strName = macid.getText().toString();
				String []  arr = { String.valueOf(APID), strName};				
				intent.putExtra("arr", arr);
				WSTActivity.this.finish();
				startActivity(intent);				
			}});
		
		move2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
//				wifi.End();
				Intent intent = new Intent(WSTActivity.this,SeekAp.class);
				String strName = macid.getText().toString();
				String []  arr = { String.valueOf(APID2), strName};				
				intent.putExtra("arr", arr);
				WSTActivity.this.finish();
				startActivity(intent);	
				
			}});
		
		
	}
	

	/* Request updates at startup */
    @Override
    protected void onResume() {
      super.onResume();
      //locationManager.requestLocationUpdates(provider, 400, 1, this);
    }

    /* Remove the locationlistener updates when Activity is paused */
    @Override
    protected void onPause() {
      super.onPause();
      //locationManager.removeUpdates(this);
    }
    
	@Override
	public void onStop() {
		super.onStop();
		wifi.End();
	}
    

	public void UpdateListWifi(List<ScanResult> results)
	{
		arraylist.clear(); 
		//wifi.StartScan();
	    for (ScanResult result : results) {
            HashMap<String, String> item = new HashMap<String, String>();                       
            item.put(ITEM_KEY, result.BSSID + ":" + result.SSID + ":" + result.level + ":" + result.frequency);
            //item.put(ITEM_KEY, result.SSID);
            arraylist.add(item);
		    }
		    adapter.notifyDataSetChanged(); 
    }
	
	public void GetWifiInfo(List<ScanResult> results)
		{
			HashMap<String, String> item = new HashMap<String, String>();
			item.get(ITEM_KEY);
	    }
		
    public void clickHandlerForUpdateWifiButton(View v) {
    	wifi.StartScan();
    	//UpdateListWifi(results);
    }

}
